package com.pineapple.module.web.hithormone;

import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.pineapple.module.core.hitfertilizer.domain.Hitfertilizer;
import com.pineapple.module.core.hithormone.domain.Hithormone;
import com.pineapple.module.core.hithormone.domain.HithormoneCriteria;
import com.pineapple.module.core.hithormone.domain.HithormoneModel;
import com.pineapple.module.core.hithormone.service.HithormoneManager;
import com.pineapple.module.util.connection.DBCPConnection;

public class HithormoneAction extends ActionSupport implements ModelDriven<HithormoneModel> {

	private static final long serialVersionUID = -3661000161146714152L;
	HithormoneModel model = new HithormoneModel();


	public HithormoneAction(){
		try(Connection conn = new DBCPConnection().getConnection()){
				HithormoneManager manager = new HithormoneManager();
				model.setLstHormone(manager.hormoneCombo(conn));
				model.setLstArea(manager.areaCombo(conn));
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	public String init(){
		HttpSession session = ServletActionContext.getRequest().getSession();
        if(session.getAttribute("user")==null){
        	return "init";
        }
		SimpleDateFormat thaiFormat = new SimpleDateFormat("dd/MM/yyyy",new Locale("en","EN"));
		Date date = new Date();
		String sysDate = thaiFormat.format(date);
		model.getCriteria().setHithormoneDateFrom(sysDate);
		model.getCriteria().setHithormoneDateTo(sysDate);
		return "search";
	}


	public String search(){
		HttpSession session = ServletActionContext.getRequest().getSession();
        if(session.getAttribute("user")==null){
        	return "init";
        }
		String criteriaKey = "";
		HithormoneCriteria criteria = model.getCriteria();

		try(Connection conn = new DBCPConnection().getConnection()){
			HithormoneManager manager = new HithormoneManager();

			if (model.getCriteriaKey() != null && !model.getCriteriaKey().equals("")) {
				criteria = (HithormoneCriteria) session.getAttribute(model.getCriteriaKey());
				/*int startrecord = model.getStart();
				criteria.setStart(startrecord);*/
				criteriaKey = model.getCriteriaKey();
				model.setCriteria(criteria);
			} else {
				Calendar calendar = Calendar.getInstance();
				criteriaKey = String.valueOf(calendar.getTimeInMillis());
				criteria.setCriteriaKey(criteriaKey);
			}
			model.setLstSearch(manager.search(model.getCriteria(), conn));
			if (model.getLstSearch() == null) {
				ServletActionContext.getRequest().setAttribute("message",getText("0002"));
			}
			model.setCriteria(criteria);
			model.setCriteriaKey(criteriaKey);
			session.setAttribute(criteriaKey, criteria);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return"search";
	}

	public String gotoAdd(){
		HttpSession session = ServletActionContext.getRequest().getSession();
        if(session.getAttribute("user")==null){
        	return "init";
        }
			model.setPage("add");
			//set sysdate
			SimpleDateFormat thaiFormat = new SimpleDateFormat("dd/MM/yyyy",new Locale("en","EN"));
			String sysDate = thaiFormat.format(new Date());
			model.getHithormone().setHithormoneDate(sysDate);
		return "addEdit";
	}

	public String add(){
		HttpSession session = ServletActionContext.getRequest().getSession();
        if(session.getAttribute("user")==null){
        	return "init";
        }
		try(Connection conn = new DBCPConnection().getConnection()){
			HithormoneManager manager = new HithormoneManager();
			String a []  = model.getHithormone().getHithormoneArea().split(",");
			model.getHithormone().setHithormoneAreaId(a[0]);
			model.getHithormone().setPlantingStatus(a[1]);

			manager.add(conn, model.getHithormone());
			model.setHithormone(new Hithormone());
			model.setLstArea(manager.areaCombo(conn));
			ServletActionContext.getRequest().setAttribute("message",getText("0004"));
			model.getHithormone().setHithormoneNumb(String.valueOf(manager.countNumb(conn, a[0])));
		}catch(Exception ex){
			ServletActionContext.getRequest().setAttribute("message",getText("00011"));
			ex.printStackTrace();
		}
		return "addEdit";
	}

	public String edit(){
		HttpSession session = ServletActionContext.getRequest().getSession();
        if(session.getAttribute("user")==null){
        	return "init";
        }
		try(Connection conn = new DBCPConnection().getConnection()){
			HithormoneManager manager = new HithormoneManager();
			String a []  = model.getHithormone().getHithormoneArea().split(",");
			model.getHithormone().setHithormoneArea(a[0]);
			manager.edit(conn, model.getHithormone());
			model.setHithormone(new Hithormone());
			model.setLstArea(manager.areaCombo(conn));
			ServletActionContext.getRequest().setAttribute("message",getText("0006"));
		}catch(Exception ex){
			ServletActionContext.getRequest().setAttribute("message",getText("0005"));
			ex.printStackTrace();
		}
		return "searchDo";
	}

	public String searchById(){
		HttpSession session = ServletActionContext.getRequest().getSession();
        if(session.getAttribute("user")==null){
        	return "init";
        }
		try(Connection conn = new DBCPConnection().getConnection()){
			int id = 0;
			if(ServletActionContext.getRequest().getParameter("hitIds") != null){
				id = Integer.parseInt(ServletActionContext.getRequest().getParameter("hitIds"));
			}

			if(ServletActionContext.getRequest().getParameter("page") != null){
				String page = ServletActionContext.getRequest().getParameter("page");
				model.setPage(page);
				HithormoneManager manager = new HithormoneManager();
				model.setHithormone(manager.searchById(conn,id));
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return "addEdit";
	}

	public String delete(){
		HttpSession session = ServletActionContext.getRequest().getSession();
        if(session.getAttribute("user")==null){
        	return "init";
        }
		String ids = "";
		if(ServletActionContext.getRequest().getParameter("hitIds") != null){
			ids = ServletActionContext.getRequest().getParameter("hitIds");
		}
		try(Connection conn = new DBCPConnection().getConnection()){
				HithormoneManager manager = new HithormoneManager();
				String id[] = ids.split(",");
				String a[] = null;
				String b[] = null;
				String c[] = null;
				for (int i = 0; i < id.length; i++) {
					a =  id[i].split("pp");
					b =  a[1].split("dd");
					c =  id[i].split("dd");
					manager.delete(conn,a[0].trim(),b[0].trim(),Integer.parseInt(c[1].trim()));
				}
				ServletActionContext.getRequest().setAttribute("message",getText("0009"));
		}catch(Exception ex){
			ex.printStackTrace();
			ServletActionContext.getRequest().setAttribute("message",getText("0010"));
		}
		return "searchDo";
	}

	public String clear(){
		HttpSession session = ServletActionContext.getRequest().getSession();
        if(session.getAttribute("user")==null){
        	return "init";
        }
		model.setCriteria(new HithormoneCriteria());
		return "search";
	}

	@Override
	public HithormoneModel getModel() {
		// TODO Auto-generated method stub
		return model;
	}


}
